<template>
  <div class="login-page">
    <h1>登录/注册</h1>
    <div v-if="!isRegister">
      <h2>登录</h2>
      <form @submit.prevent="login">
        <input v-model="loginEmail" type="email" placeholder="电子邮件" required />
        <input v-model="loginPassword" type="password" placeholder="密码" required />
        <button type="submit">登录</button>
      </form>
      <p>还没有账号？ <a href="#" @click="toggleRegister">注册</a></p>
      <p><a href="#" @click="goToResetPassword">忘记密码？</a></p>
      <p><a href="#" @click="goToAdminLogin">管理员登录</a></p>
    </div>
    <div v-else>
      <h2>注册</h2>
      <form @submit.prevent="register">
        <input v-model="registerFirstName" type="text" placeholder="名字" required />
        <input v-model="registerLastName" type="text" placeholder="姓氏" required />
        <input v-model="registerEmail" type="email" placeholder="电子邮件" required />
        <input v-model="registerPassword" type="password" placeholder="密码" required />
        <button type="submit">注册</button>
      </form>
      <p>已有账号？ <a href="#" @click="toggleRegister">登录</a></p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isRegister: false,
      loginEmail: '',
      loginPassword: '',
      registerFirstName: '',
      registerLastName: '',
      registerEmail: '',
      registerPassword: ''
    };
  },
  methods: {
    toggleRegister() {
      this.isRegister = !this.isRegister;
    },
    login() {
      // 从localStorage获取用户数据
      const users = JSON.parse(localStorage.getItem('users') || '[]')
      const user = users.find(u => u.email === this.loginEmail && u.password === this.loginPassword)
      
      if (user) {
        localStorage.setItem('isLoggedIn', 'true')
        this.$router.push('/')
      } else {
        alert('邮箱或密码错误')
      }
    },
    register() {
      // 存储用户数据到localStorage
      const users = JSON.parse(localStorage.getItem('users') || '[]')
      
      // 检查邮箱是否已注册
      if (users.some(u => u.email === this.registerEmail)) {
        alert('该邮箱已注册')
        return
      }
      
      // 添加新用户
      users.push({
        firstName: this.registerFirstName,
        lastName: this.registerLastName,
        email: this.registerEmail,
        password: this.registerPassword
      })
      
      localStorage.setItem('users', JSON.stringify(users))
      alert('注册成功！请登录')
      this.isRegister = false
    },
    goToResetPassword() {
      // 跳转到重置密码页面
    },
    goToAdminLogin() {
      this.$router.push('/admin/login')
    }
  }
};
</script>

<style scoped>
.login-page {
  max-width: 400px;
  margin: 80px auto;
  padding: 40px;
  text-align: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.login-page h1 {
  color: #2c3e50;
  margin-bottom: 30px;
  font-weight: 600;
}

.login-page h2 {
  color: #34495e;
  margin-bottom: 20px;
}

.login-page form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.login-page input {
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.login-page input:focus {
  border-color: #3498db;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
  outline: none;
}

.login-page button {
  padding: 12px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-page button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.login-page a {
  color: #3498db;
  text-decoration: none;
  transition: all 0.3s ease;
}

.login-page a:hover {
  color: #2980b9;
  text-decoration: underline;
}

.login-page p {
  margin-top: 15px;
  color: #7f8c8d;
}
</style>